package edu.jkd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import edu.jkd.entity.*;
import edu.jkd.service.*;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private IProductService productService;
    /**
     * 查询所有影片信息，前八位展示在首页
     * @return 返回ModelAndView
     */
    @GetMapping()
    public ModelAndView getAllProducts(){
        ModelAndView mav=new ModelAndView();
        //调用service查询
        List<Product> productList = productService.list();
        //数据绑定
        mav.addObject("productList",productList);
        //页面跳转
        mav.setViewName("index");
        return mav;

    }
    @Autowired
    private IFilmService filmService;
    @Autowired
    private ITypeService typeService;
    @Autowired
    private ITfService tfService;

    /**
     * 根据id获取product信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ModelAndView getById(@PathVariable("id") Integer id){
        Product product=productService.getById(id);
        LambdaQueryWrapper<Film> queryWrapper1=new LambdaQueryWrapper<>();
        queryWrapper1.eq(Film::getFilmProid,id);
        Film film = filmService.getOne(queryWrapper1);
        String ctime=film.getFilmCtime().toString().replace("T"," ");
        String duration=film.getFilmDuration().toString().replace(".0"," ");

        LambdaQueryWrapper<Tf> queryWrapper5=new LambdaQueryWrapper<>();
        queryWrapper5.eq(Tf::getfId,film.getFilmId());
        List<Tf> tfList = tfService.list(queryWrapper5);

        List<Type> typeList =new ArrayList<Type>();
        for (int i=0;i<tfList.size();i++) {
            Type type=typeService.getById(tfList.get(i).gettId());
            typeList.add(type);
        }
        ModelAndView mav=new ModelAndView("detail");
        mav.addObject("product",product);
        mav.addObject("ctime",ctime);
        mav.addObject("duration",duration);
        mav.addObject("film",film);
        mav.addObject("typeList",typeList);
        return mav;
    }

    /**
     * 保存product信息
     */
    @PostMapping()
    public void save(){
        Product product=new Product();
        product.setProName("万里归途");
        product.setProImg("images/m2.jpg");
        product.setProPrice(new BigDecimal(56));
        product.setProDesc("万里归途");
        productService.save(product);
    }

}
